package com.example.lee.Controller;

import com.Domain.commonResult;
import com.Domain.payment;
import com.Service.paymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class paymentController {
    @Autowired
    paymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    //传给前端JSON
    @PostMapping(value = "/payment/create")    //写操作POST
    public commonResult create(@RequestBody payment payment){

        //由于在mapper.xml配置了useGeneratedKeys="true" keyProperty="id"，会将自增的id封装到实体类中
        int result = paymentService.create(payment);

        log.info("*****插入结果：" + result);

        if(result > 0){
            //restful风格：对象以json串返回到页面
            return new commonResult(200, "插入数据库成功,serverPort："+serverPort, result);
        }else {
            return new commonResult(444,"插入数据库失败", null);
        }
    }

    //传给前端JSON
    @GetMapping(value = "/payment/get/{id}")    //写操作POST
    public commonResult getPaymentById(@PathVariable("id") Long id){

        payment payment = paymentService.getPaymentById(id);

        log.info("*****查询结果：" + payment);

        if(payment != null){
            return new commonResult(200, "查询数据库成功,serverPort："+serverPort, payment);
        }else {
            return new commonResult(444,"查询ID:"+id+"没有对应记录", null);
        }
    }

    @GetMapping("/payment/lb")
    public String getPaymentLB(){
        return serverPort;
    }
}
